const { DataTypes } = require("sequelize");
const User = require("../model/user.model");

const seq = require("../db/seq");
const Information = require("./information.model");
const Category = require("./category.model");

// 创建模型(Model zd_user -> 表 zd_users)
const Comment = seq.define(
  "comment",
  {
    // id 会被sequelize自动创建, 管理
      cat_id: {
          type: DataTypes.INTEGER,
          allowNull: true,
          comment: "项目id",
      },
      info_id: {
          type: DataTypes.INTEGER,
          allowNull: true,
          comment: "资讯Id",
      },
    comment_content: {
      type: DataTypes.TEXT,
      allowNull: false,
      comment: "评论内容",
    },
    user_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: "评论人id",
    },
    parent_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: "评论回复id",
      defaultValue: 0,
    },
  },
  {
    paranoid: true,
  }
);
Comment.belongsTo(User, {
  foreignKey: "user_id",
  as: "user_info",
});
Comment.belongsTo(Information, {
    foreignKey: "info_id",
    as: "information_info",
});
Comment.belongsTo(Category, {
    foreignKey: "cat_id",
    as: "category_info",
});
Comment.belongsTo(Comment, {
    foreignKey: "parent_id",
    as: "child_comment",
});
// 强制同步数据库(创建数据表)
// Comment.sync({ force: true });

module.exports = Comment;
